package thread;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/4/28
 * Time:16:15
 * ++/--操作
 **/
public class ThreadDemo21 {
    static class Counter {
        //任务执行次数
        private final int maxSize = 100000;

        //num++
        public int increment() {
            int num1 = 0;
            for (int i = 0; i < maxSize; i++) {
                num1++;
            }
            return num1;
        }

        //num--
        public int decrement() {
            int num2 = 0;
            for (int i = 0; i < maxSize; i++) {
                num2--;
            }
            return num2;
        }
    }

    private static int num1 = 0;
    private static int num2 = 0;

    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Thread t1 = new Thread(() -> {
            num1 = counter.increment();
        });
        t1.start();
        Thread t2 = new Thread(() -> {
            num2 = counter.decrement();
        });
        t2.start();

        t1.join();
        t2.join();

        System.out.println("最终执行结果" + (num1 + num2));
    }
}
